package org.bogusware.json.editor;

import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextDoubleClickStrategy;
import org.eclipse.jface.text.TextAttribute;
import org.eclipse.jface.text.formatter.ContentFormatter;
import org.eclipse.jface.text.formatter.IContentFormatter;
import org.eclipse.jface.text.presentation.IPresentationReconciler;
import org.eclipse.jface.text.presentation.PresentationReconciler;
import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
import org.eclipse.jface.text.rules.Token;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.SourceViewerConfiguration;

public class JSONConfiguration extends SourceViewerConfiguration {
	private JSONDoubleClickStrategy doubleClickStrategy;
	private JSONObjectScanner tagScanner;
	private JSONScanner scanner;
	private ColorManager colorManager;

	public JSONConfiguration(ColorManager colorManager) {
		this.colorManager = colorManager;
	}

	public String[] getConfiguredContentTypes(ISourceViewer sourceViewer) {
		return new String[] { IDocument.DEFAULT_CONTENT_TYPE,
				JSONPartitionScanner.JSON_COMMENT,
				JSONPartitionScanner.JSON_OBJECT };
	}

	public ITextDoubleClickStrategy getDoubleClickStrategy(ISourceViewer sourceViewer, String contentType) {
		if (doubleClickStrategy == null)
			doubleClickStrategy = new JSONDoubleClickStrategy();
		return doubleClickStrategy;
	}

	protected JSONScanner getJSONScanner() {
		if (scanner == null) {
			scanner = new JSONScanner(colorManager);
			scanner.setDefaultReturnToken(new Token(new TextAttribute(colorManager.getColor(IJSONColorConstants.DEFAULT))));
		}
		return scanner;
	}

	protected JSONObjectScanner getJSONObjectScanner() {
		if (tagScanner == null) {
			tagScanner = new JSONObjectScanner(colorManager);
			tagScanner.setDefaultReturnToken(new Token(new TextAttribute(colorManager.getColor(IJSONColorConstants.OBJECT))));
		}
		return tagScanner;
	}

	public IPresentationReconciler getPresentationReconciler(ISourceViewer sourceViewer) {
		PresentationReconciler reconciler = new PresentationReconciler();

		DefaultDamagerRepairer dr = new DefaultDamagerRepairer(getJSONObjectScanner());
		reconciler.setDamager(dr, JSONPartitionScanner.JSON_OBJECT);
		reconciler.setRepairer(dr, JSONPartitionScanner.JSON_OBJECT);

		dr = new DefaultDamagerRepairer(getJSONScanner());
		reconciler.setDamager(dr, IDocument.DEFAULT_CONTENT_TYPE);
		reconciler.setRepairer(dr, IDocument.DEFAULT_CONTENT_TYPE);

		NonRuleBasedDamagerRepairer ndr = new NonRuleBasedDamagerRepairer(new TextAttribute(colorManager.getColor(IJSONColorConstants.JSON_COMMENT)));
		reconciler.setDamager(ndr, JSONPartitionScanner.JSON_COMMENT);
		reconciler.setRepairer(ndr, JSONPartitionScanner.JSON_COMMENT);

		return reconciler;
	}

	@Override
	public IContentFormatter getContentFormatter(ISourceViewer sourceViewer) {
		ContentFormatter formatter = new ContentFormatter();
		formatter.setFormattingStrategy(new JSONObjectFormattingStrategy(), JSONPartitionScanner.JSON_OBJECT);
		return super.getContentFormatter(sourceViewer);
	}
}